package com.haian.slidealbum.model.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.haian.slidealbum.R;
import com.haian.slidealbum.common.UtilsCommon;
import com.haian.slidealbum.control.AlbumManageActivity;
import com.haian.slidealbum.model.object.ObjectAlbum;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

public class AdapterAlbumManage extends BaseAdapter {
	private Activity mActivity;
	private ArrayList<ObjectAlbum> listAlbum;
	private ImageLoader imageLoader;
	private DisplayImageOptions options;

	@SuppressWarnings("deprecation")
	public AdapterAlbumManage(Activity _mActivity,
			ArrayList<ObjectAlbum> _listAlbum) {
		this.mActivity = _mActivity;
		this.listAlbum = _listAlbum;

		imageLoader = ImageLoader.getInstance();
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				mActivity).threadPriority(Thread.NORM_PRIORITY - 2)
				.denyCacheImageMultipleSizesInMemory()
				.discCacheFileNameGenerator(new Md5FileNameGenerator())
				.tasksProcessingOrder(QueueProcessingType.LIFO).build();
		imageLoader.init(config);
		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.ic_launcher)
				.showImageForEmptyUri(R.drawable.ic_launcher)
				.showImageOnFail(R.drawable.ic_launcher).cacheInMemory()
				.cacheOnDisc().resetViewBeforeLoading(true) // default
				.imageScaleType(ImageScaleType.IN_SAMPLE_INT) // default
				.bitmapConfig(Bitmap.Config.RGB_565) // default
				.displayer(new FadeInBitmapDisplayer(300)) // default
				.handler(new Handler()) // default
				.build();
	}

	@Override
	public int getCount() {
		return listAlbum.size();
	}

	@Override
	public Object getItem(int position) {
		return listAlbum.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View view = convertView;
		final Holder holder;
		if (view == null) {
			LayoutInflater inflater = mActivity.getLayoutInflater();
			view = inflater.inflate(R.layout.item_album, parent, false);

			holder = new Holder();
			holder.itemImage = (ImageView) view.findViewById(R.id.itemImage);
			holder.itemName = (TextView) view.findViewById(R.id.itemName);
			holder.itemCheckbox = (ImageView) view.findViewById(R.id.itemCheckbox);
			holder.itemCheckboxSelected = (ImageView) view.findViewById(R.id.itemCheckboxSelected);
			holder.rlItem = (RelativeLayout)view.findViewById(R.id.rlItem);
			view.setTag(holder);
		} else {
			holder = (Holder) view.getTag();
		}
		final ObjectAlbum objectAlbum = listAlbum.get(position);
		holder.itemName.setText(objectAlbum.getNameAlbum().toString());

		// set size item
		Point size = UtilsCommon.getSizeScreen(mActivity);
		holder.itemImage.getLayoutParams().height = size.x / 2;

		String urlImg = "";
		try {
			urlImg = objectAlbum.getObjectImageCover().getUrlImage();
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		if(urlImg.equals("") == false)
		{
			imageLoader.displayImage("file:/"+ urlImg, holder.itemImage, options);
		}
		
		
		if(objectAlbum.isCheckDelete())
		{
			holder.itemCheckbox.setVisibility(View.VISIBLE);
			holder.itemCheckboxSelected.setVisibility(View.INVISIBLE);
			if(objectAlbum.isSelected())
			{
				holder.itemCheckbox.setVisibility(View.INVISIBLE);
				holder.itemCheckboxSelected.setVisibility(View.VISIBLE);
			}else{
				holder.itemCheckbox.setVisibility(View.VISIBLE);
				holder.itemCheckboxSelected.setVisibility(View.INVISIBLE);
			}
			holder.rlItem.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if(objectAlbum.isCheckDelete())
					{
						if(objectAlbum.isSelected())
						{
							holder.itemCheckbox.setVisibility(View.VISIBLE);
							holder.itemCheckboxSelected.setVisibility(View.INVISIBLE);
							objectAlbum.setSelected(false);
						}else{
							holder.itemCheckbox.setVisibility(View.INVISIBLE);
							holder.itemCheckboxSelected.setVisibility(View.VISIBLE);
							objectAlbum.setSelected(true);
						}
						((AlbumManageActivity)mActivity).getListAlbumSelected();
					}
				}
			});
		}else{
			holder.itemCheckbox.setVisibility(View.INVISIBLE);
			holder.itemCheckboxSelected.setVisibility(View.INVISIBLE);
		}
		return view;
	}

	static class Holder {
		ImageView itemImage, itemCheckbox, itemCheckboxSelected;
		RelativeLayout rlItem;
		TextView itemName;
	}
}
